package cn.cloudplug.ui.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import cn.cloudplug.core.util.MyUtil;
import cn.cloudplug.ui.R;

/**
 * Created by pc on 2018/1/29.
 */

public class TurnStyleMenu4 extends RelativeLayout {

    private int radius;
    private int startAngle=0;
    private int initStartAngle=0;
    private int angle;
    private LinkedHashMap<OptionItem,View> subActionMap = new LinkedHashMap<>();
    private ArrayList<OptionItem> subActionItems = new ArrayList<>();

    private OnMenuItemClickListener onMenuItemClickListener=null;
    private Point center;
    private final int check=2;

    public TurnStyleMenu4(Context context) {
        super(context);
        init();
    }

    public TurnStyleMenu4(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void scrollTo(final int to){
        if(check !=to){
            ObjectAnimator animator = ObjectAnimator.ofInt(this,"startAngle" , startAngle, -to*angle);
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    
                    int x = Math.abs(to-check);
                    startAngle=-2*angle;
                    if(to>check){
                        for (int i = 0; i < x; i++) {
                            subActionItems.add(subActionItems.remove(0));
                        }
                    }else{
                        for (int i = 0; i < x; i++) {
                            subActionItems.add(0, subActionItems.remove(subActionItems.size() - 1));
                        }
                    }
                    invalidateItem();

                }

            });
            animator.setDuration(500);
            animator.start();
        }
    }
    public void setStartAngle(int i){
        startAngle=i;
        invalidateItem();
    }
    //添加item
    public void addItem(OptionItem item){
        LayoutInflater inflate = LayoutInflater.from(getContext());
        View view = inflate.inflate(R.layout.item_menu,null);

        subActionMap.put(item,view);
        subActionItems.add(item);

        ImageView imageView = view.findViewById(R.id.iv_icon);
        TextView tv_title1 = view.findViewById(R.id.tv_zh);
        TextView tv_title2 = view.findViewById(R.id.tv_en);
        int imgid = item.getImgId();
        String title1 = item.getZh();
        String title2 = item.getEn();
        imageView.setImageResource(imgid);
        tv_title1.setText(title1);
        tv_title2.setText(title2);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollTo(subActionItems.indexOf(v.getTag()));
                if(onMenuItemClickListener!=null){
                    onMenuItemClickListener.onMenuItemClick((OptionItem) v.getTag());
                }
            }
        });
        imageView.setTag(item);
        this.addView(view);
    }

    public void centre(Point point){
        center=point;
    }

    public void invalidateItem(){

        invalidateItem(center,radius,angle,startAngle);
    }

    public void  angle(int angle){
        this.angle=angle;
        initStartAngle=-2*angle;
        setStartAngle(initStartAngle);

    }


    public void  radius(int radius){
        this.radius=radius;
    }

    private void init(){
        radius=300;
        angle = 30;
        center = new Point(0,0);
    }

    private void invalidateItem(Point center,int radius,int angle,int startAngle) {
        RectF area = new RectF(center.x - radius, center.y - radius, center.x + radius, center.y + radius);

        Path orbit = new Path();
        orbit.addArc(area, startAngle, angle*(subActionMap.size()-1));
        PathMeasure measure = new PathMeasure(orbit, false);

        int divisor;
        if(Math.abs(angle*(subActionMap.size()-1)) >= 360 || subActionMap.size() <= 1) {
            divisor = subActionMap.size();
        }
        else {
            divisor = subActionMap.size() -1;
        }
        for(int i=0; i<subActionMap.size(); i++) {
            float[] coords = new float[] {0f, 0f};
            measure.getPosTan((i) * measure.getLength() / divisor, coords, null);
            int x=(int) coords[0] -  MyUtil.DipToPixels(getContext(),20);
            int y=(int) coords[1] -  MyUtil.DipToPixels(getContext(),20);
            View view = subActionMap.get(subActionItems.get(i));
            view.setTranslationX(x);
            view.setTranslationY(y);
            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }


    public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) {
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    interface OnMenuItemClickListener{
        void onMenuItemClick(OptionItem item);
    }
}
